﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Crgd.UAuthServer.T4.Helpers
{
    public static class NameHelper
    {
        public static string GetEntityClassName(string tablename, bool isReadonly, string prefix, string connector = null)
        {
            if (string.IsNullOrEmpty(prefix) || !tablename.StartsWith(prefix))
            {
                if (isReadonly)
                {
                    return WordsToTitleCase(tablename, connector) + "View";
                }
                return WordsToTitleCase(tablename, connector);
            }
            if (isReadonly)
            {
                return WordsToTitleCase(tablename.Remove(0, prefix.Length), connector) + "View";
            }
            else
            {
                return WordsToTitleCase(tablename.Remove(0, prefix.Length), connector);
            }
        }

        public static string GetDtoClassName(string tablename, bool isReadonly, string prefix, string connector = null)
        {
            if (string.IsNullOrEmpty(prefix) || !tablename.StartsWith(prefix))
            {
                if (isReadonly)
                {
                    return WordsToTitleCase(tablename, connector) + "ViewDto";
                }
                return WordsToTitleCase(tablename, connector);
            }
            if (isReadonly)
            {
                return WordsToTitleCase(tablename.Remove(0, prefix.Length), connector) + "ViewDto";
            }
            else
            {
                return WordsToTitleCase(tablename.Remove(0, prefix.Length), connector) + "Dto";
            }
        }

        public static string GetConfigurationClassName(string tablename, bool isReadonly, string prefix, string connector = null)
        {
            if (string.IsNullOrEmpty(prefix) || !tablename.StartsWith(prefix))
            {
                if (isReadonly)
                {
                    return WordsToTitleCase(tablename, connector) + "ViewConfiguration";
                }
                return WordsToTitleCase(tablename, connector);
            }
            if (isReadonly)
            {
                return WordsToTitleCase(tablename.Remove(0, prefix.Length), connector) + "ViewConfiguration";
            }
            else
            {
                return WordsToTitleCase(tablename.Remove(0, prefix.Length), connector) + "Configuration";
            }
        }

        public static string GetPropertyName(string columnName, string prefix, string connector = null)
        {
            if (string.IsNullOrWhiteSpace(prefix) || !columnName.StartsWith(prefix))
            {
                return WordsToTitleCase(columnName, connector);
            }
            return WordsToTitleCase(columnName.Remove(0, prefix.Length));
        }

        private static string WordsToTitleCase(string value, string connector = null)
        {
            if (string.IsNullOrEmpty(connector))
            {
                return value;
            }

            var resultValue = new StringBuilder();
            var values = value.Split(new string[] { connector }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var v in values)
            {
                resultValue.Append(Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(v));
            }

            return resultValue.ToString();
        }
    }
}
